﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using roysland.net.designpatterns.MVVM;
using roysland.net.xcom.core.Helpers;
using roysland.net.xcom.core.Models;

namespace roysland.net.xcom.editor.ViewModel
{
    public sealed class ItemEditorViewModel : WorkspaceViewModel
    {
        private ObservableCollection<ObDataRecordViewModel> _obDataRecords;

        public ItemEditorViewModel()
        {
            DisplayName = "Item Editor";
            InitializeData();
        }

        private void InitializeData()
        {
            var records = OBDataParser.GetRecords();
            var list = new ObservableCollection<ObDataRecordViewModel>();
            foreach (var record in records)
            {
                list.Add(new ObDataRecordViewModel(record));
            }

            ObDataRecords = list;
        }

        public void Save()
        {
            var list = new List<ObDataRecord>();
            foreach (var r in ObDataRecords)
            {
                list.Add(r.Record);
            }
            OBDataParser.SaveObData(list);
        }

        #region Properties
        public ObservableCollection<ObDataRecordViewModel> ObDataRecords
        {
            get { return _obDataRecords; }
            set
            {
                if (value == _obDataRecords)
                    return;

                _obDataRecords = value;

                OnPropertyChanged("ObDataRecords");
            }
        }
        #endregion
    }
}
